package org.cnc.explain.lifecycle;

import org.cnc.explain.util.StepCount;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.ReaderHelper;

/**
 * <p>
 * 
 * </p>
 *
 * @author zhiyong.tan
 * @since 2022-08-02
 */
@Component
public class LifecycleInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		if(LifeCycleBean.BEAN_NAME.equals(beanName)){
			System.out.println("\n\n");
			ReaderHelper.beanLifecycleTrace ( StepCount.getStep() + "bean [%s] 实例化前置: " ,beanName);
			ReaderHelper.formatTrace();
			System.out.println("\n\n");
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if(LifeCycleBean.BEAN_NAME.equals(beanName)){
			System.out.println("\n\n");
			ReaderHelper.beanLifecycleTrace ( StepCount.getStep() + "bean [%s] 实例化后置: " ,beanName);
			ReaderHelper.formatTrace();
			System.out.println("\n\n");
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		if(LifeCycleBean.BEAN_NAME.equals(beanName)){
			System.out.println("\n\n");
			ReaderHelper.beanLifecycleTrace ( StepCount.getStep() + "bean [%s] 属性设置后置: " ,beanName);
			ReaderHelper.formatTrace();
			System.out.println("\n\n");
		}
		return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
	}
}
